package com.samoiloff.ui.btns.check {
	import flash.display.MovieClip;
	import flash.events.MouseEvent;

	/**
	 * @author Alexander Slavshik <as.kvint@gmail.com>
	 * created 15.03.2010
	 */
	public class RadioButtonController extends CheckBoxController {
		private static var _groups:Array = [];
		private var _group:String;
		private var _selected:Boolean;
		
		public function RadioButtonController(mc:MovieClip, groupName:String = 'defaultGroup', caption:String = 'RadioButton') {
			super(mc, caption);
			_group = groupName;
			if(_groups[_group] == null) {
				_groups[_group] = [];
			}
			_groups[_group].push(this);
		}

		override protected function clickHandler(event:MouseEvent):void {
			selected = true;
		}

		public function get selected():Boolean {
			return _selected;
		}
		
		public function set selected(selected:Boolean):void {
			_selected = selected;
			super.checked = _selected;
			var arr:Array = _groups[_group];
			for (var i:int = 0;i < arr.length;i++) {
				if(arr[i] != this){
					arr[i].checked = !selected;
				}
			}
		}

		override public function destroy():void {
			super.destroy();
			var arr:Array = _groups[_group];
			if(arr != null){
				var idx:int = arr.indexOf(this);
				if(idx != -1){
					arr.splice(idx, 1);
				}
			}
		}
	}
}
